<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use app\common\model\Special as SpecialModel;
use app\common\model\SearchLog;
use fast\Payment;
class Special extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
       
        if($this->request->isAjax()){
            $params = $this->request->post();
            $where['status'] = 'normal';
            $where['is_audit'] = 1;
            $where['start_time'] = ['<',time()];
            if(!empty($params['type'])){
                $where['type_id'] = $params['type'];
            }
            if(!empty($params['keywords'])){
                $searchlog = SearchLog::get(['name'=>$params['keywords']]);
                if(empty($searchlog)){
                    $searchlog = new Searchlog();
                    $searchlog->name = $params['keywords'];
                    $searchlog->nums = 1;
                    
                }else{
                    $searchlog->nums = $searchlog['nums'] + 1;
                }
                $searchlog->save();
                $where['title'] = ['like','%'.$params['keywords'].'%'];
            }
            if(!empty($params['city'])){
                $where['city'] = $params['city'];
            }
           $list = Db::name('Special')->where($where)->order('id desc')->page($params['page'],10)->select();
           foreach ($list as $key => $value) {
                $week = date('w',$value['start_time']);
                switch ($week) {
                    case '0':
                        $list[$key]['start_time'] = date('Y-m-d',$value['start_time']).' 周日 '.date('H:i',$value['start_time']);
                        break;
                    case '1':
                        $list[$key]['start_time'] = date('Y-m-d',$value['start_time']).' 周日 '.date('H:i',$value['start_time']);
                        break;
                    case '2':
                        $list[$key]['start_time'] = date('Y-m-d',$value['start_time']).' 周日 '.date('H:i',$value['start_time']);
                        break;
                    case '3':
                        $list[$key]['start_time'] = date('Y-m-d',$value['start_time']).' 周日 '.date('H:i',$value['start_time']);
                        break; 
                    case '4':
                        $list[$key]['start_time'] = date('Y-m-d',$value['start_time']).' 周日 '.date('H:i',$value['start_time']);
                        break;
                    case '5':
                        $list[$key]['start_time'] = date('Y-m-d',$value['start_time']).' 周日 '.date('H:i',$value['start_time']);
                        break;
                    case '6':
                        $list[$key]['start_time'] = date('Y-m-d',$value['start_time']).' 周日 '.date('H:i',$value['start_time']);
                        break;
                }
                $list[$key]['type_name'] = Db::name('Sptype')->where('id',$value['type_id'])->value('name');
                $list[$key]['user'] = Db::name('User')->where('id',$value['user_id'])->field('nickname,avatar')->find();
           }
           return json(['code'=>200,'data'=>$list,'msg'=>'success']);
        }
        return $this->view->fetch();
    }
    
    public function detail(){
        
        $id = $this->request->get('id');
        $info = Db::name('Special')->where('id',$id)->find();
        $info['type_name'] = Db::name('Sptype')->where('id',$info['type_id'])->value('name');
        $info['user'] = Db::name('User')->where('id',$info['user_id'])->field('id,nickname,avatar')->find();
        $week = date('w',$info['start_time']);
        switch ($week) {
            case '0':
                $start = date('Y-m-d',$info['start_time']).' 周日 '.date('H:i',$info['start_time']);
                break;
            case '1':
                $start = date('Y-m-d',$info['start_time']).' 周日 '.date('H:i',$info['start_time']);
                break;
            case '2':
                $start = date('Y-m-d',$info['start_time']).' 周日 '.date('H:i',$info['start_time']);
                break;
            case '3':
                $start = date('Y-m-d',$info['start_time']).' 周日 '.date('H:i',$info['start_time']);
                break; 
            case '4':
                $start = date('Y-m-d',$info['start_time']).' 周日 '.date('H:i',$info['start_time']);
                break;
            case '5':
                $start = date('Y-m-d',$info['start_time']).' 周日 '.date('H:i',$info['start_time']);
                break;
            case '6':
                $start = date('Y-m-d',$info['start_time']).' 周日 '.date('H:i',$info['start_time']);
                break;
        }

        $info['start_time'] = $start;
        $this->assign('info',$info);
        $this->assign('id',$id);
        return $this->view->fetch();
    }
    public function add(){
        if($this->request->isAjax()){
            $params = $this->request->post('row/a');
            Db::startTrans();
            try {
                $params['user_id'] = (int)$this->auth->id;
                $params['start_time'] = strtotime($params['start_time']);
                $params['status'] = 'normal';
                $area = explode('/',$params['area']);
                $params['district'] = $area[2];
                $params['city'] = $area[1];
                $params['province'] = $area[0];
                SpecialModel::create($params,true);
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();   
        }
        $typelist = Db::name('Sptype')->where('status','normal')->column('name','id');
        $this->assign('typelist',$typelist);
        return $this->view->fetch();
    }
    public function baoming($id=null){
        if($this->request->isAjax()){
            $params = $this->request->post();
            $special = Db::name('Special')->where('id',$params['id'])->find();
            if($params['pay'] == 'wechat'){
                $result = Payment::wechat($special['id'],'special',$special['price'],$this->auth->id,json_encode($params,1));
            }
            if($params['pay'] == 'alipay'){
                $result = Payment::alipay($special['id'],'special',$special['price'],$this->auth->id,json_encode($params,1));
            }
            if(!$result){
                $this->error('创建支付订单失败');
            }else{
                $this->success('','',$result);
            }
        }
        $adviser = Db::name('Special')->where('id',$id)->find();
        $user = Db::name('User')->where('id',$this->auth->id)->find();
        $this->assign('adviser', $adviser);
        $this->assign('user', $user);
        return $this->view->fetch();
    }
}
